package cn.dtsmusic.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import cn.dtsmusic.model.domain.City;
import cn.dtsmusic.plugin.GlobalExceptionHandler;
import cn.dtsmusic.service.CityService;
import cn.dtsmusic.util.NetworkUtils;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class IndexController {
	
	private static Log log = LogFactory.getLog(IndexController.class );
	
	 @Autowired
	    private CityService cityService;


	 @GetMapping("/")
     public ResponseEntity<String> welcome(HttpServletRequest request) {
		 String access = printAccess(request);
         return ResponseEntity.ok("Welcome!"+"\r\n"+access);
     }
	 
	 private  String printAccess(HttpServletRequest request) {
	        StringBuilder su = new StringBuilder();
	        su.append("\nUser-Access-Args:").append("{");
	        su.append("\"protocol\":\"").append(request.getProtocol() + "(" + request.getScheme()).append(")\",");
	        su.append("\"ip\":\"").append(NetworkUtils.getIpAddr(request)).append("\",");
	        su.append("\"port\":\"").append(NetworkUtils.getPort(request)).append("\",");
	        su.append("\"method\":\"").append(request.getMethod()).append("\",");
	        su.append("\"url\":\"").append(NetworkUtils.getCurrentURL(request)).append("\",");
	        su.append("\"user-agent\":\"").append(NetworkUtils.getUserAgent(request)).append("\",");
	        su.append("\"uAT\":\"").append(request.getAttribute("uAT")).append("\"}");
	        log.info(su.toString());
	        
	        return su.toString() ;
	    }
	 
		
}
